package Reflection;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream.GetField;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Properties;

public class Main {

	@SuppressWarnings("unchecked")
	public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException, NoSuchFieldException {
		// TODO Auto-generated method stub
		
		// 反射是框架的灵魂，底层机制的基石。
		// 反射加载类是 动态加载
		
		// 加载完类之后，在堆中产生类的class对象
		// 反射机制是java实现动态语言的关键，也就是通过反射实现类动态加载
		// java.lang.reflect.Method 反射相关的类 动态代理
		// 代码阶段/编译阶段 -》 class类阶段（加载阶段） -》  Runtime运行（运行阶段）
		Properties properties = new Properties();
		properties.load(new FileInputStream("src\\reflection.properties"));
		String path = properties.get("path").toString();
		String method = properties.get("method").toString();
		
		System.out.println("path = " + path);
		System.out.println("method = " + method);
		
		Class cls = Class.forName(path);
		Object object = cls.newInstance();
		
		Method method2 = cls.getMethod(method);
		method2.invoke(object);
		
		Field age = cls.getField("age");
		System.out.println("age = " + age.get(object));
		
		
		
		
		
//		Properties properties = new Properties();
//		properties.load(new FileInputStream("src\\reflection.properties"));
//		String path = properties.get("path").toString();
//		String method = properties.get("method").toString();
//		System.out.println("path = "+ path);
//		System.out.println("method = " + method);
//		
//		// 1. 加载类
//		Class cls = Class.forName(path);
//		
//		Object o = cls.newInstance();
//		System.out.println("o的运行类型 = " + o.getClass());
//		
//		Method method1 = cls.getMethod(method);
//		method1.invoke(o);
 	}

}
